package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TBCustomerMapper;
import com.ruoyi.system.domain.TBCustomer;
import com.ruoyi.system.service.ITBCustomerService;

import javax.validation.Validator;

/**
 * 客户Service业务层处理
 * 
 * @author pspyy
 * @date 2022-02-24
 */
@Service
public class TBCustomerServiceImpl implements ITBCustomerService 
{
    private static final Logger log = LoggerFactory.getLogger(TBCustomerServiceImpl.class);
    @Autowired
    private TBCustomerMapper tBCustomerMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询客户
     * 
     * @param id 客户主键
     * @return 客户
     */
    @Override
    public TBCustomer selectTBCustomerById(Long id)
    {
        return tBCustomerMapper.selectTBCustomerById(id);
    }

    /**
     * 查询客户列表
     * 
     * @param tBCustomer 客户
     * @return 客户
     */
    @Override
    public List<TBCustomer> selectTBCustomerList(TBCustomer tBCustomer)
    {
        return tBCustomerMapper.selectTBCustomerList(tBCustomer);
    }

    /**
     * 新增客户
     * 
     * @param tBCustomer 客户
     * @return 结果
     */
    @Override
    public int insertTBCustomer(TBCustomer tBCustomer)
    {
        tBCustomer.setCreateTime(DateUtils.getNowDate());
        return tBCustomerMapper.insertTBCustomer(tBCustomer);
    }

    /**
     * 修改客户
     * 
     * @param tBCustomer 客户
     * @return 结果
     */
    @Override
    public int updateTBCustomer(TBCustomer tBCustomer)
    {
        tBCustomer.setUpdateTime(DateUtils.getNowDate());
        return tBCustomerMapper.updateTBCustomer(tBCustomer);
    }

    /**
     * 批量删除客户
     * 
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteTBCustomerByIds(Long[] ids)
    {
        return tBCustomerMapper.deleteTBCustomerByIds(ids);
    }

    /**
     * 删除客户信息
     * 
     * @param id 客户主键
     * @return 结果
     */
    @Override
    public int deleteTBCustomerById(Long id)
    {
        return tBCustomerMapper.deleteTBCustomerById(id);
    }

    @Override
    public String importCustomer(List<TBCustomer> customerList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(customerList) || customerList.size() == 0)
        {
            throw new ServiceException("导入客户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TBCustomer customer : customerList)
        {
            try
            {
                // 验证是否存在这个客户
                TBCustomer cus = tBCustomerMapper.selectCustomerByName(customer.getCustomerName());
                if (StringUtils.isNull(cus))
                {
                    BeanValidators.validateWithException(validator, customer);
                    this.insertTBCustomer(customer);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户 " + customer.getCustomerName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, customer);
                    this.updateTBCustomer(customer);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户 " + customer.getCustomerName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、客户 " + customer.getCustomerName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、客户 " + customer.getCustomerName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public String checkCustomerNameUnique(String customerName) {
        int i = tBCustomerMapper.checkCustomerNameUnique(customerName);
        if(i > 0){
            return "0";
        }
        return "1";
    }

    @Override
    public TBCustomer selectCustomerByName(String customerName) {
        return tBCustomerMapper.selectCustomerByName(customerName);
    }
}
